﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Configuration;
using System.Threading;

namespace Interfleet.SE.UICompiler.Core.ClosureCompiler
{
    /// <summary>
    /// This class executes the call to the Closure Compiler Java component.
    /// </summary>
    public class CCCompilerAgent : ProcessCompilerAgent
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="args"><see cref="Arguments"/> for the compiler.</param>
        internal CCCompilerAgent(Arguments args)
            : this(
            args, 
            new CCArgumentBuilder(args), 
            new ProcessStartInfoBuilder("java"),
            new CCResultBuilder(args)
            )
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="args">Arguments for the compiler.</param>
        /// <param name="argumentBuilder">Argument builder</param>
        /// <param name="processStartInfoBuilder">An implementation of <see cref="IProcessStartInfoBuilder"/></param>
        /// <param name="resultBuilder"></param>
        public CCCompilerAgent(
            Arguments args, 
            ArgumentBuilder argumentBuilder, 
            IProcessStartInfoBuilder processStartInfoBuilder,
            ResultBuilder resultBuilder)
            : base(args, argumentBuilder, resultBuilder, processStartInfoBuilder)
        {

        }

        /// <summary>
        /// Compiles using the Google Closure Compiler jar -archive.
        /// </summary>
        /// <returns>A <see cref="CompileResult"/></returns>
        public override CompileResult Compile()
        {
            if (StartInfoBuilder == null)
            {
                throw new InvalidOperationException("StartInfoBuilder cannot be null");
            }
            CompileResult result = new CompileResult(Arguments.Files, Arguments.CompiledFileName);
            result.Success = false;
            // The default behaviour of this method is to invoke a
            // process with arguments and read the status from the
            // error stream.

            string resultFromCompiler;
            var compilationStartTime = DateTime.Now;
            using (var process = new Process())
            {
                var arguments = ArgumentBuilder.Build();
                var startInfo = StartInfoBuilder.Build(arguments);
                process.StartInfo = startInfo;
                process.Start();
                // max time (milliseconds) to wait for the process to exit
                process.WaitForExit(Arguments.CompileTimeout ?? Arguments.DefaultCompileTimeout);

                // the compiler (jar archive) seems to write all
                // status information on error stream
                using (StreamReader stdErr = process.StandardError)
                {
                    resultFromCompiler = stdErr.ReadToEnd();
                }
            }
            return ResultBuilder.Build(resultFromCompiler, compilationStartTime);
        }
    }
}
